package top.yifengxiner.blockchain.service.Impl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import top.yifengxiner.blockchain.pojo.vo.BLoginBody;
import top.yifengxiner.blockchain.service.BLoginService;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.common.utils.TokenUtils;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/12 10:02
 * @description：登录服务
 * @modified By：
 * @version: 0.0.1$
 */
@Service
public class BLoginServiceImpl implements BLoginService
{
    @Override
    public AjaxResult login(BLoginBody loginBody)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        UsernamePasswordToken token = new UsernamePasswordToken(loginBody.getSubject(),loginBody.getPassword());
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
            String tk = TokenUtils.createToken(loginBody.getSubject());
            System.out.println(tk);
            ajaxResult.put("token",tk);
            return ajaxResult;
        }
        catch (AuthenticationException e)
        {
            return AjaxResult.error("用户名密码错误");
        }
    }
}
